﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class ChiTietHoaDonDAO
    {
        public static ChiTietHoaDonDTO LayChiTietHoaDonTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            CHI_TIET_HOA_DON chiTietHoaDon = myDB.CHI_TIET_HOA_DONs.Single(c => c.ID == id);
            return new ChiTietHoaDonDTO(chiTietHoaDon.MaSanPham,
                chiTietHoaDon.MaHoaDon, chiTietHoaDon.DonGia.Value, chiTietHoaDon.SoLuongSanPham.Value);
        }

        public static ChiTietHoaDonDTO LayChiTietHoaDonTheoMaHoaDon(int mahoadon)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            CHI_TIET_HOA_DON chiTietHoaDon = myDB.CHI_TIET_HOA_DONs.Single(c => c.ID == mahoadon);
            return new ChiTietHoaDonDTO(chiTietHoaDon.MaSanPham,
                chiTietHoaDon.MaHoaDon, chiTietHoaDon.DonGia.Value, chiTietHoaDon.SoLuongSanPham.Value);
        }

        public static bool ThemChiTietHoaDon(ChiTietHoaDonDTO chitiethoadon)
        {
            CHI_TIET_HOA_DON cthd = ChuyenTuchitiethoadonDTOSangchitiethoadon(chitiethoadon);
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            try
            {
                myDB.CHI_TIET_HOA_DONs.InsertOnSubmit(cthd);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            
        }

        public static bool XoaChiTietHoaDonTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            CHI_TIET_HOA_DON cthd = myDB.CHI_TIET_HOA_DONs.Single(c => c.ID == id);
            try
            {
                myDB.CHI_TIET_HOA_DONs.DeleteOnSubmit(cthd);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool XoaDanhSachChiTietHoaDonTheoMaHoaDon(int mahoadon)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var listcthd = from chitiethoadon in myDB.CHI_TIET_HOA_DONs
                           where chitiethoadon.MaHoaDon == mahoadon
                           select chitiethoadon;
            try
            {
                myDB.CHI_TIET_HOA_DONs.DeleteAllOnSubmit(listcthd);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool XoaChiTietHoaDonTheoMaHoaDon(int mahoadon)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            CHI_TIET_HOA_DON cthd = myDB.CHI_TIET_HOA_DONs.Single(c => c.MaHoaDon == mahoadon);
            try
            {
                myDB.CHI_TIET_HOA_DONs.DeleteOnSubmit(cthd);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        #region 2   -   Function Helper

        public static ChiTietHoaDonDTO ChuyenTuChiTietHoaDonSangChiTietHoaDonDTO(CHI_TIET_HOA_DON chitiethoadon)
        {
            return new ChiTietHoaDonDTO(chitiethoadon.MaSanPham, chitiethoadon.MaHoaDon, chitiethoadon.DonGia.Value, chitiethoadon.SoLuongSanPham.Value);
        }

        public static CHI_TIET_HOA_DON ChuyenTuchitiethoadonDTOSangchitiethoadon(ChiTietHoaDonDTO chitiethoadonDTO)
        {
            CHI_TIET_HOA_DON result = new CHI_TIET_HOA_DON();
            result.ID = chitiethoadonDTO.ID;
            result.MaSanPham = chitiethoadonDTO.MaSanPham;
            result.MaHoaDon = chitiethoadonDTO.MaHoaDon;
            result.DonGia = chitiethoadonDTO.DonGia;
            result.SoLuongSanPham = chitiethoadonDTO.SoLuongSanPham;            
            return result;
        }
        #endregion
    }
}
